#!/home/y/bin/perl

use FindBin qw/$Bin/;
use lib qw(lib ../lib);
use Data::Dumper qw(Dumper);
use Test::More qw/no_plan/;
use Test::Exception;

use Farsail::Config;

use_ok('Farsail::ActionSet');

my $rootdir = "$Bin/conf";

my $actset = Farsail::ActionSet->new(
    Farsail::Config->new($rootdir)
);
isa_ok($actset, "Farsail::ActionSet");

is_deeply(
    [ sort keys %{$actset->{actions}} ],
    [
        'depot',
        'global',
        'noah',
        'olympia'
    ],
    'actions()');

is_deeply(
    $actset->{INCLUDES},
    {
        "$rootdir/app/olympia.act" => 1,
        "$rootdir/plugin/depot.act" => 1,
        "$rootdir/farsail.act" => 1,
        "$rootdir/app/noah.act" => 1
    }, "check inclues"
);

$actset->load_action("$Bin/conf/plugin/plugin.act");
is_deeply(
    $actset->{actions}{plugin},
    {
        'foo' => {
            'module' => 'Farsail::Plugin::Foo',
            'depends' => [
                'bar'
            ]
        },
    },
    'load_action($yaml_file)');

$actset->load_action(
    {
        'plugin' => {
            'bar' => {
                args => { date => 'date' },
                'module' => 'Farsail::Plugin::Bar',
            },
        }
    }
);
is_deeply(
    $actset->{actions}{plugin}{bar},
    {
        'module' => 'Farsail::Plugin::Bar',
        args => { date => 'date' },
    },
    'load_action($hash_ref)');

$actset->register_action(
    'baz',
    { namespace => 'plugin',
      args => { name => 'string' } });
is_deeply(
    $actset->{actions}{plugin}{baz},
    { namespace => 'plugin',
      args => { name => 'string' } },
    'register_action with namespace');

dies_ok { $actset->register_action() } 'call register_action with wrong args';

$actset->register_action(
    'baz',
    { args => { name => 'string' } });
is_deeply(
    $actset->{actions}{global}{baz},
    { args => { name => 'string' } },
    'register_action without namespace');
        
ok($actset->has_action('depot.etl'), 'has_action($exists_action)');
ok(!$actset->has_action('depot.any'), 'has_action($not_exists_action)');

isa_ok($actset->action('depot.etl'), 'Farsail::Action', 'action');

is_deeply(
    $actset->requires('noah.daily'),
    {
        'Farsail::App::Noah' => 1,
        'Farsail::App::Depot' => 1,
    }, 'requires'
);

is_deeply(
    $actset->args('noah.daily'),
    {
        'date' => {
            'type' => 'date',
            'default' => 'yesterday'
        },
        'dir' => {
            'type' => 'string'
        }
    }, "args");

$actset->use_namespace('noah');
is_deeply(
    $actset->action('daily'),
    $actset->action('noah.daily'),
    'use_namespace',
);
